package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by wolfcode on 2018/8/15.
 */
@Controller
public class CatalogController {
    @Reference
    private ICatalogService catalogService;
    @RequestMapping("/catalog")
    public String catalogPage(Model model){
        model.addAttribute("allCatalogJson", catalogService.selectAllFromCache());
        return "catalog/catalog";
    }
    @RequestMapping("/catalog/getChildCatalog")
    public String getChildCatalog(Long catalogId,Model model){
        model.addAttribute("catalogList",catalogService.queryByPId(catalogId));
        return "catalog/child_catalog";
    }
    @RequestMapping("/catalog/updateSort")
    @ResponseBody
    public JSONResultVo updateSort(@RequestBody List<Long> ids){
        JSONResultVo vo = new JSONResultVo();
        try{
            catalogService.updateSort(ids);
        }catch(Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }
    @RequestMapping("/catalog/save")
    @ResponseBody
    public JSONResultVo saveOrUpdate(Catalog catalog){
        JSONResultVo vo = new JSONResultVo();
        try{
            catalogService.saveOrUpdate(catalog);
        }catch(Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }
    @RequestMapping("/catalog/delete")
    @ResponseBody
    public JSONResultVo delete(Long catalogId){
        JSONResultVo vo = new JSONResultVo();
        try{
            catalogService.delete(catalogId);
        }catch(Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }
}
